/*
 * @Description: 文件内容描述
 * @Author: 李志杰
 * @Date: 2020-03-17 13:39:14
 * @LastEditTime: 2020-03-25 00:43:43
 * @LastEditors: huacong
 */
import _ from 'lodash';
import * as api from '@/services/ReportMgr';
import * as utils from '../utils/treeUtils';

const Model = {
  namespace: 'RemovalRateReport',
  state: {
    all: [],
    items: [],
    expandedRowKeys: [],
    level: 'project',
  },
  effects: {
    *init({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { all: [], items: [], expandedRowKeys: [] },
      });
      const response = yield call(api.removalReportUsingGET, payload);
      const level = yield select(state => state.RemovalRateReport.level);
      if (response && response.code === 1) {
        console.time('执行时间all');
        console.log(response.data);
        const all = utils.buildRowStructure(response.data);
        console.timeEnd('执行时间all');

        console.time('执行时间levelTree');
        const levelTree = utils.levelTree(all, level);
        console.timeEnd('执行时间levelTree');

        console.time('执行时间expandedRowKeys');
        const expandedRowKeys = utils.getExpandedRowKeys(levelTree);
        console.timeEnd('执行时间expandedRowKeys');

        console.time('执行时间treeData');
        const treeData = utils.buildTree(levelTree);
        console.timeEnd('执行时间treeData');

        yield put({
          type: 'save',
          payload: { all, items: treeData, expandedRowKeys },
        });
      }
      if (callback) callback(response);
    },
    *level({ payload, callback }, { call, put, select }) {
      const all = yield select(state => state.RemovalRateReport.all);
      const { level } = payload;
      const levelTree = utils.levelTree(all, level);

      const expandedRowKeys = utils.getExpandedRowKeys(levelTree);
      const treeData = utils.buildTree(levelTree);
      yield put({
        type: 'save',
        payload: { level, items: treeData, expandedRowKeys },
      });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
